package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShopingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @RequestMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(id);

        ShoppingCart shoppingCartNew = shoppingCartService.addCart(shoppingCart);

        return R.success(shoppingCartNew);
    }

    //查询指定用户的购物车集合数据
    @RequestMapping("/list")
    public R<List<ShoppingCart>> findAllByUserId(HttpSession session){
        Long userId= (Long) session.getAttribute("user");

        List<ShoppingCart> shoppingCartList = shoppingCartService.findAllByUserId(userId);
        return R.success(shoppingCartList);
    }

    //清空购物车
    @RequestMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");

        shoppingCartService.clean(userId);

        return R.success("清空购物车成功");
    }
}
